package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.bean.AddDeviceMethodBean;
import com.qiantu.phone.glide.GlideApp;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
public final class AddDeviceStepAdapter extends AppAdapter<AddDeviceMethodBean.LinkStepsBean> {

    public AddDeviceStepAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final View topLine;
        private final View bottomLine;
        private final ImageView iconStep;
        private final TextView tvStepName;
        private final TextView tvIntroduce;

        private ViewHolder() {
            super(R.layout.item_add_step);
            topLine = findViewById(R.id.top_line);
            bottomLine = findViewById(R.id.bottom_line);
            tvStepName = findViewById(R.id.tv_step_name);
            tvIntroduce = findViewById(R.id.tv_introduce);
            iconStep = findViewById(R.id.icon_step);
        }

        @Override
        public void onBindView(int position) {
            AddDeviceMethodBean.LinkStepsBean item = getItem(position);
            tvStepName.setText(item.getTitle());
            if(TextUtils.isEmpty(item.getIntroduce())){
                tvIntroduce.setVisibility(View.GONE);
            }else{
                tvIntroduce.setVisibility(View.VISIBLE);
                tvIntroduce.setText(item.getIntroduce());
            }
            GlideApp.with(iconStep).load(BuildConfig.APP_IMAGE_BASE +"app/images/icon/"+ item.getLogo()).placeholder(new ColorDrawable(0x000000000)).error(new ColorDrawable(0x000000000)).into(iconStep);
            if(position == 0){
                topLine.setVisibility(View.GONE);
                bottomLine.setVisibility(View.VISIBLE);
            }else if(position == getData().size() - 1){
                topLine.setVisibility(View.VISIBLE);
                bottomLine.setVisibility(View.GONE);
            }else{
                topLine.setVisibility(View.VISIBLE);
                bottomLine.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}